'use strict';
app.controller('goodsListCtrl',['$scope','toaster','$stateParams','utilService','goodsListService','$q','$modal',function($scope,toaster,$stateParams,utilService,goodsListService,$q,$modal){
    $scope.goodsListVO={};
    $scope.goodsListVO.shopId=$stateParams.shopId;
    //查询店铺所有产品
    $scope.findPageAll=function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain+'shop/goods/list',
            params:$scope.goodsListVO
        }).then(function(result){
        });
    }
    $scope.findPageAll();

    //更新上下架,申请再次审核
    $scope.updateStatus=function(id,status){
        var shopId=$scope.goodsListVO.shopId;
        goodsListService.updateStatus($scope,id,status,shopId).then(function (result) {
                switch (result.status){
                    case 200:
                        $scope.types=result.data;
                        $scope.findPageAll();
                        toaster.pop('success', "提示", "操作成功");
                        break;
                    case 677:
                    case 678:
                        toaster.pop('waiting', "提示", result.message);
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
        })
    }


    //导出产品列表
    $scope.exportGoodsList = function(){
        openModal("确定要导出店铺所有产品吗？" +
            "(导出时间跟导出数据量的大小有关，请耐心等待)");
        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"温馨提示",
                controller:"exportGoodsListCtrl",
                message:msg,
                data: $scope.goodsListVO
            });
        }
    }

    //打开查看服务框
    $scope.checkReserve = function (id) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/goods/reserve_info.html',
            controller: 'reserveInfoCtrl',
            size: 'md',
            scope:$scope,
            resolve: {
                data: function () {
                    return id;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            console.info('Modal dismissed at: ' + new Date());
        });
    };

}]);

app.controller('reserveInfoCtrl',['$scope','toaster','data','$modalInstance','goodsListService',
    function($scope,toaster,data,$modalInstance,goodsListService){
    $scope.id=data;
    $scope.goodsInfo={};

    $scope.checkReserve=function(){
        goodsListService.checkReserve($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.goodsInfo=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    };

    $scope.checkReserve();

    $scope.ok = function(){
        $modalInstance.dismiss('cancel');
    };

}]);

app.controller('exportGoodsListCtrl', ['$scope', '$modalInstance','$state','goodsListService','toaster','modalData',
    function($scope, $modalInstance,$state,goodsListService,toaster,modalData) {
        $scope.modal = modalData;
        $scope.param = modalData.data;
        $scope.AllowToDelete=true;
        $scope.ok=function(){
            if(!$scope.AllowToDelete){
                return;
            }
            $scope.AllowToDelete=false;
            goodsListService.exportData($scope);
            $modalInstance.dismiss('success');
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);